<?php
class CmsController extends Controller
{
	public $layout='//layouts/cms_layout';
	public $showTree = false;
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
	
	
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('login','logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('help','module','user','index', 'ajaxFillTree','settings','addhtml','addimage'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionsimpletree()
    {
        Yii::import('application.extensions.SimpleTreeWidget');    
        SimpleTreeWidget::performAjax();
        
    }
    public function actiongetContent($id)
    {
        //echo "ID : ".$id;
        if($_GET['id'] > 0)
        {
        	$model = new Content;
            if(isset($id)){
                $model = $model->findByPk($id);
            }
            if(isset($_POST['Content']))
            {
                $model->attributes=$_POST['Content'];
                if($model->validate()){
                    $model->save();
                }
            }
            $findConditions = isset($id) ? "id != $id" : null;
            $parentList = Content::model()->findAll($findConditions);
            $newParentList = array(''=>'root');
            if(!empty($parentList)){
                $parentList = CHtml::listData($parentList, 'id', 'name');
                //$newParentList = array_push($newParentList, $parentList);
                $newParentList = $newParentList+ $parentList;
            }
            
            $contentTypes = CHtml::listData(ContentType::model()->findAll(), 'id', 'name');
            $applications = CHtml::listData(Application::model()->findAll(), 'id', 'name');
            $contentStatuses = CHtml::listData(ContentStatus::model()->findAll(), 'id', 'name');
            $viewVars = array(
                'model'=>$model,
                'parentList'=>$newParentList,
                'contentTypes'=>$contentTypes,
                'applications'=>$applications,
                'contentStatuses'=>$contentStatuses
            );
            
        	$this->renderPartial('../content/edit',$viewVars, false, true);
/*
            if($id==11){
                $page = Application::model()->findByPk(1);
                $this->renderPartial('settings',array('model'=>$page),false,true);
            }elseif ($id==3 || $id==13){
                $page = new User;
                $this->renderPartial('../user/index',array('model'=>$page),false,true);
            }elseif ($id==14){
                $page = new User;
                $this->renderPartial('../user/create',array('model'=>$page),false,true);
            }	*/
        } 
        else
        {    
            $this->renderPartial('../page/_getting_started');
        }
            
    }
     
	public function actionIndex()
	{
		/*if(Yii::app()->user->isGuest)
			$this->redirect('/cms/login');
		else */
	    $this->showTree = true;
        $this->render('/cms/index');
	}

	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->createUrl('/video'));
				
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}
    
    public function actionLogout()
	{
	  
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->createUrl('cms/login'));
	}
	
	public function actionHelp()
	{
		var_dump(Yii::app()->modules);
		$this->render('help');
	}
	
	public function actionModule()
	{
		$model = Module::model()->findAll(); // Todo get only authorized module per application...		
		
		if(isset($_POST['fileUpload']))
		{			
			Yii::app()->moduleInstaller->install(CUploadedFile::getInstanceByName('upload'));
			$this->redirect('/cms/module');
		}		
		$this->render('module', array('model'=>$model));
	}
	
	public function actionSettings()
	{
		$appId = 1; // Todo - Should be dynamic
		$model = Application::model()->findByPk($appId);
        if(isset($_POST['ajax']) && $_POST['ajax']==='content-edit-form')
		{
            $model->attributes=$_POST['Application'];
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		else if(isset($_POST['Application']))
		{
			$model->attributes=$_POST['Application'];
			// validate user input and redirect to the previous page if valid
			 if($model->validate()){
	                $model->save();
	          }				
		}		
		$this->render('settings', array('model' => $model));
	}
    
    public function actionAddHtml()
    {
        if(isset($_POST['Content']['id']) && $_POST['Content']['id']!='')
        {
            $model = Content::model()->findByPk($_POST['Content']['id']);
        }
        else
        $model = new Content;
        
        if(isset($_POST['ajax']) && $_POST['ajax']==='page-edit-form')
		{
            $model->attributes=$_POST['Content'];
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		else if(isset($_POST['Content']))
		{
			$model->attributes=$_POST['Content'];
            ///need to change /////////
            $model->parent_id = 55;
            $model->application_id =1 ;
            $model->content_type_id=1;
            ///need to change ////////
            $model->created_by = Yii::app()->user->id;
            
			// validate user input and redirect to the previous page if valid
			 if($model->validate()){
	                $model->save();
	          }				
		}		
        $this->render('addhtml', array('model' => $model));
    }
    
    public function actionAddImage()
    {
        $model = new Content;
        $this->render('addimage', array('model' => $model));
    }
}
?>